var url = "https://route.showapi.com/213-4";
var urlLrc = "https://route.showapi.com/213-2"
var searchUrl = "https://route.showapi.com/213-1"
var myFn = {
	getMyDate: function() {
		var d = new Date();
		var month = d.getMonth() + 1;
		month = month < 10 ? ("0" + month) : month;
		var myDate = d.getDate() < 10 ? ("0" + d.getDate()) : d.getDate();
		var hours = d.getHours() < 10 ? ("0" + d.getHours()) : d.getHours();
		var minutes = d.getMinutes() < 10 ? ("0" + d.getMinutes()) : d.getMinutes();
		var seconds = d.getSeconds() < 10 ? ("0" + d.getSeconds()) : d.getSeconds();
		return "" + d.getFullYear() + month + myDate + hours + minutes + seconds;
	}
}

angular.module('starter.services', [])
	.service("playlists", ["$http", "$rootScope", "$ionicLoading", function($http, $rootScope, $ionicLoading) {
		return {
			getId: function() {
				$ionicLoading.show({
					template: '加载中...'
				}, 1000 * 10)
				$http.get("json/data.json").success(function(data) {
					console.log(data);
					$rootScope.$broadcast("mytop", data.id);
				})
			}
		}

	}])
	.service('playlist', ['$http', '$rootScope', "$ionicLoading", function($http, $rootScope, $ionicLoading) {
		return {
			"playlist": function(id) {
				$ionicLoading.show({
					template: '加载中...'
				}, 1000 * 10)
				$http.get(url, {
					params: {
						"showapi_timestamp": myFn.getMyDate(),
						"showapi_appid": "33493",
						"showapi_sign": '9f45ddb0339f43d9a7eba16a51838b53',
						"topid": id
					}
				}).then(function(data) {
					$rootScope.$broadcast("playlist", data.data.showapi_res_body.pagebean.songlist);
				})
			},
			"getLrc": function(id) {
				$http.get(urlLrc, {
					params: {
						"showapi_timestamp": myFn.getMyDate(),
						"showapi_appid": "33493",
						"showapi_sign": '9f45ddb0339f43d9a7eba16a51838b53',
						"musicid": id,
					}
				}).then(function(data) {
					$rootScope.$broadcast("mlyric", data.data.showapi_res_body.lyric);
				})
			},
			"searchSong": function(keyWord) {
				console.log(keyWord)
				$http.get(searchUrl, {
					params: {
						"showapi_timestamp": myFn.getMyDate(),
						"showapi_appid": "33493",
						"showapi_sign": '9f45ddb0339f43d9a7eba16a51838b53',
						"keyword": keyWord
					}
				}).then(function(data) {
					console.log(data)
					$rootScope.$broadcast("searchData", data.data.showapi_res_body.pagebean.contentlist);
				})
			}
		}
	}])